package controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;

import javax.swing.JFrame;

import model.*;

public class Parser {
//parses files and directories
	//private Host currentHost;
	private Controller controller;
	private String currentCommand;
	private Scan currentScan;
	Parser(Controller controller){
		this.controller = controller;
	}
	public void parseFiles(File[] files, JFrame frame){
		for (File file: files){
			checkFile(file, frame);
		}
	}
	private void checkFile(File file, JFrame frame){
		if(file.isDirectory()){
			File[] children = file.listFiles();
			for (File child: children){
				checkFile(child, frame);
			}
		}
		else {
			if (file.isFile() && file.canRead()){
				parseFile(file);
			}
		}
	}
	private void parseFile(File file){
		
		try {
			BufferedReader in = new BufferedReader(new FileReader(file));
			String line = in.readLine();
			if (isNMapFile(line)){
				
				while (line!=null){
					processLine(line);
					line = in.readLine();
				}
				
			}
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	private boolean isNMapFile(String line){
		//determines if the file is an NMap text file based on the beginning string of characters
		if (line != null){
			if (line.length()>6){
				if (line.substring(0, 7).equals("# Nmap ")){
					currentCommand = line; 
					return true;
				}
			}
		}
		currentCommand = "";
		return false;
	}
	private void processLine(String line){
		String[] tokens = split(line);
		//checks to see if the line is
		
		//host enumeration
		if (tokens.length>4 &&
				tokens[0].equals("Nmap") &&
				tokens[1].equals("scan") &&
				tokens[2].equals("report")){
			String IPAddress = tokens[tokens.length-1];
			if (IPAddress.length()>2){
				//if formatted like "(10.10.10.10)"
				if (IPAddress.charAt(0)=='('){
					IPAddress = IPAddress.substring(1, IPAddress.length()-1);
				}
			}
			//assign the current Scan object
				
			currentScan = controller.addHost(IPAddress, currentCommand);
			
		}
		//adding port to host
		else if(tokens.length>2 && tokens[1].equals("open")){
			//parse the first token
			String protocol = tokens[0].split("/")[1];
			int portNumber = Integer.parseInt(tokens[0].split("/")[0]);
			
			PortEntry port = new PortEntry(portNumber, protocol, tokens[2]);
			currentScan.addPortEntry(port);
		}
	}
	private String[] split(String line) {
		LinkedList<String> split = new LinkedList<String>();
		String str = "";
		char[] charArray = line.toCharArray();
		int i=0;
		while (i< charArray.length){
			if (charArray[i] !=' '){
				str += charArray[i];
			}
			else{
				if (!str.equals("")){
					split.add(str);
					str = new String ("");
				}
			}
			i++;
		}
		
		if (!str.equals("")){
			split.add(str);
		}
		return split.toArray(new String[0]);
	}
	
}
